<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>发布商品</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <base href="http://localhost/campus/">
    <link rel="shortcut icon" href="static/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" type="text/css" href="layui/css/layui.css"/>
    <link rel="stylesheet" type="text/css" href="static/css/main.css"/>
    <script src="static/js/jquery.min.js"></script>
    <script src="static/js/main.js"></script>
    <script src="layui/layui.js"></script>
</head>
<body>
<div class="layui-fluid">
    <jsp:include page="/pages/common/header.jsp"/>

    <%--主体内容--%>
    <div class="layui-row center" style="width: 100%;display: flex;">
        <div class="layui-col-xs2 layui-col-sm2 layui-col-md2">
        </div>
        <div class="layui-col-xs9 layui-col-sm9 layui-col-md9">
            <div id="center_body" style="width: 100%;">
                <div class="layui-progress" lay-filter="uploadImgProgress"
                     style="height:3px;display:none;">
                    <div class="layui-progress-bar layui-bg-blue" style="height: 3px;" lay-percent="0%"></div>
                </div>
                <form class="layui-form layui-form-pane" lay-filter="product-form"
                      action="product/${product!=null?"update":"add"}" method="post">
                    <input title="" name="id" value="${product.id}" style="display: none;width: 0;height: 0;">
                    <div class="release_body">
                        <div class="layui-form-item release_input" pane>
                            <label class="layui-form-label">商品名</label>
                            <div class="layui-input-block">
                                <input type="text" name="name" lay-verify="name" placeholder="请输入商品名称"
                                       autocomplete="off" class="layui-input" value="${product!=null? product.name:""}">
                            </div>
                        </div>
                        <div class="layui-form-item release_input">
                            <label class="layui-form-label">商品分类</label>
                            <div class="layui-input-inline release_item">
                                <select id="firstCategorySelect" title="" lay-filter="firstCategorySelect"
                                        name="firstCategory"
                                        lay-verify="firstCategory">
                                    <c:if test="${product == null}">
                                        <option value="0">请选择大类</option>
                                    </c:if>
                                    <c:forEach items="${categories}" var="firstCategory">
                                        <c:if test="${product == null || product !=null && !product.myCategory.parent.equals(firstCategory.id)}">
                                            <option value="${firstCategory.id}">${firstCategory.title}</option>
                                        </c:if>
                                        <c:if test="${product !=null && product.myCategory.parent.equals(firstCategory.id)}">
                                            <option value="${firstCategory.id}" selected>${firstCategory.title}</option>
                                        </c:if>
                                    </c:forEach>
                                </select>
                            </div>
                            <div class="layui-input-inline release_item">
                                <select name="category" id="secondCategorySelect" lay-filter="secondCategorySelect"
                                        lay-verify="secondCategory">
                                    <option value="${product!=null?product.myCategory.id:"0"}" id="secondCategoryOption"
                                            selected>${product!=null?product.myCategory.categoryName:"请选择小类"}</option>
                                </select>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">商品价格</label>
                                <div class="layui-input-inline" style="width: 100px;">
                                    <input type="text" name="price" placeholder="￥" autocomplete="off"
                                           class="layui-input" lay-verify="price"
                                           value="${product!=null? product.price:""}">
                                </div>
                            </div>
                        </div>

                        <div class="layui-form-item layui-form-text release_input">
                            <label class="layui-form-label">商品描述</label>
                            <div class="layui-input-block">
                            <textarea title="" id="product_describe" placeholder="请输入对商品的简单描述" class="layui-textarea"
                                      name="productDescribe"
                                      lay-verify="describe">${product!=null? product.productDescribe:""}</textarea>
                            </div>
                        </div>
                        <div class=" layui-form-item layui-form-text release_input" pane>
                            <label class="layui-form-label">商品图片</label>
                            <div class="layui-input-block" style="margin-top: 6%">
                                <div class="layui-textarea">
                                    <img alt="" style="width: 60px;height: 60px;margin: 10px;" id="productImgUrl"
                                         src="${product!=null?product.img:"static/img/add.png"}">
                                    <input title="" id="productImgUrlInput" name="img"
                                           value="${product!=null?product.img:""}" style="display: none;"/>
                                </div>
                            </div>
                        </div>
                        <div style="margin-right: 10%;float: right;">
                            <button id="reset" class="layui-btn layui-btn-primary">重置</button>
                            <button class="layui-btn layui-btn-normal" lay-submit
                                    lay-filter="release">发布
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
        <div class="layui-col-xs1 layui-col-sm1 layui-col-md1">
        </div>
    </div>

    <%@include file="/pages/common/footer.jsp" %>

<p id="flag_isDisplayNav" style="display: none;">0</p>
</div>
<script>

    var element = null;
    layui.use('element', function () {
        element = layui.element;

        //…
    });

    var form = null;
    layui.use('form', function () {
        form = layui.form;

        //表达验证
        form.verify({
            name: function (value) { //value：表单的值、item：表单的DOM对象
                if (value === "") {
                    return '商品名不能为空';
                }
                if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
                    return '商品名不能有特殊字符';
                }
                if (/(^\_)|(\__)|(\_+$)/.test(value)) {
                    return '商品名首尾不能出现下划线\'_\'';
                }
                if (/^\d+\d+\d$/.test(value)) {
                    return '商品名不能全为数字';
                }
            }
            , firstCategory: function (value) {
                if (value === "0") {
                    return '请为商品选择一个分类';
                }
            }
            , secondCategory: function (value) {
                if (value === "0") {
                    return '请为商品选择一个具体的分类';
                }
            }
            , describe: function (value) {
                if (value === "")
                    return "对商品进行简单的描述"
            }
            , price: [
                /^(([^0][0-9]+|0)\.([0-9]{1,2})$)|^(([^0][0-9]+|0)$)|^(([1-9]+)\.([0-9]{1,2})$)|^(([1-9]+)$)/
                , '请输入正确的价格'
            ]
        });

        //监听提交
        form.on('submit(release)', function (data) {
            return true;
        });

        //下拉选择框监听事件
        form.on('select(firstCategorySelect)', function (data) {
            if (data.value === "0") {
                let option = " <option value='0' id='secondCategoryOption'>请选择小类</option>";
                $('#secondCategorySelect').html(option);
                form.render();
            } else {
                $.ajax({
                    type: "GET"
                    , url: "http://localhost/campus/product/second"
                    , data: "id=" + data.value
                    , success: function (result) {
                        let option = " <option value='0' id='secondCategoryOption'>请选择小类</option>";
                        for (let i = 0; i < result.data.data.length; i++) {
                            option += " <option value='" + result.data.data[i].id + "'>" + result.data.data[i].title + "</option>";
                        }
                        $('#secondCategorySelect').html(option);
                        form.render();
                    }
                });
            }

        });

    });

    var layer = null;
    layui.use('layer', function () {
        layer = layui.layer;
        var success = "${sessionScope.backMessage.success}";

        if (success !== "") {
            let icon = 0;
            if (success === "true") icon = 1;
            else icon = 2;
            layer.msg("${sessionScope.backMessage.message}", {
                icon: icon,
                time: 1000
            });
            <%
            session.removeAttribute("backMessage");
            %>
        }
    });

    layui.use('upload', function () {
        let upload = layui.upload;


        //执行实例
        let uploadInst = upload.render({
            elem: '#productImgUrl' //绑定元素
            , url: 'http://localhost/campus/product/uploadProductImg' //上传接口
            , accept: "image"
            , acceptMime: 'image/*'
            , before: function () {
                $($('.layui-progress')[0]).css("display", "block");
            }
            , done: function (res) {
                //上传完毕回调
                $("#productImgUrl").attr("src", res.data.imgUrl);
                $("#productImgUrlInput").val(res.data.imgUrl);
                setTimeout(function () {
                    element.progress('uploadImgProgress', 0 + "%");
                    $($('.layui-progress')[0]).css("display", "none");
                }, 1000);
            }
            , error: function () {
                //请求异常回调
                layer.msg("图片上传异常", {
                    icon: 3
                    , time: 2000
                });
                setTimeout(function () {
                    element.progress('uploadImgProgress', 0 + "%");
                    $($('.layui-progress')[0]).css("display", "none");
                }, 3000);
            }
            , progress: function (n) {
                var percent = n + '%';//获取进度百分比
                element.progress('uploadImgProgress', percent); //可配合 layui 进度条元素使用
            }
        });
    });

    //重置，将表单清空
    $("#reset").click(function () {
        form.val("product-form", {
            "name": ""
            , "describe": ""
            , "price": ""
        });
        //清空文本域
        $('#product_describe').val("");
        $("#firstCategorySelect option:first").prop("selected", 'selected');
        $("#secondCategorySelect option:first").prop("selected", 'selected');
        form.render();
        return false;
    });
</script>
</body>
</html>
